package cn.itcast.ssm.util;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by wizzer on 2016/6/24.
 */

public class DateUtil {
    private static final Locale DEFAULT_LOCALE = Locale.CHINA;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取当前时间(HH:mm:ss)
     *
     * @return
     */
    public static String getDate() {

        return sdf.format(new Date());
    }


    /**
     * 通过字符串时间获取时间戳
     *
     * @param date
     * @return
     */
    public static int getTime(String date) {
        try {
            return (int) (sdf.parse(date).getTime() / 1000);
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 计算两个时间日期的分钟数
     * @param sdate
     * @param edate
     * @return
     */
    public static long getMinuteBetweens(String sdate, String edate){
        SimpleDateFormat format = sdf;
        try {
            format.setLenient(false);
            Date date1 = format.parse(sdate);
            Date date2 = format.parse(edate);
            //计算差值，分钟数
            long minutes=(date2.getTime()-date1.getTime())/(1000*60);
            return minutes;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

}
